//
//  SDEvaluateCell.m
//  FD
//
//  Created by Yasin on 2018/10/13.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  商品详情评价cell

#import "SDEvaluateCell.h"
#import "EvaluatePicItem.h" //clv的Item
#import "XHStarRateView.h" //评星
@interface SDEvaluateCell()<UICollectionViewDelegate,UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>

/**div1 一个整体*/
@property (nonatomic, strong) UIView *div1;
@property (nonatomic, strong) UIImageView *iconIV; //1.图片
@property (nonatomic, strong) UILabel *nickNameLb; //2.昵称
@property (nonatomic, strong) UILabel *timeLb; //2.时间
@property (nonatomic, strong) XHStarRateView *starView; //3. 评星 //关闭交互,不让点击


/**内容不确定,高度自适应*/
@property (nonatomic, strong) UILabel *contentLb; //4.评价内容、
/**图片clv,内容不确定,高度自适应*/
@property (nonatomic, strong) UICollectionView *picClv;
/**标记clv的数据源*/
@property (nonatomic, strong) NSMutableArray *picArrM;

/**线*/
@property (nonatomic, strong) UIView *bottomLineView;

@end

@implementation SDEvaluateCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setupView {
    
    [self.contentView addSubview:self.div1];
    [self.div1 addSubview:self.iconIV];
    [self.div1 addSubview:self.nickNameLb];
    [self.div1 addSubview:self.timeLb];
//    self.starView = [[XHStarRateView alloc] initWithFrame:CGRectMake(60, 26, 100,30)];
    self.starView.currentRating = 3;
    [self.div1 addSubview:self.starView];
    
    
    [self.contentView addSubview:self.contentLb];
    
    [self.contentView addSubview:self.picClv];
    //线
    [self.contentView addSubview:self.bottomLineView];
}


#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat sw = self.contentView.bounds.size.width;
    
    //一个整体
    self.div1.frame = CGRectMake(0, 0, sw, 60);
    self.iconIV.frame = CGRectMake(10, 10, 45, 45);
    self.nickNameLb.frame = CGRectMake(62, 5, 100, 22);
    CGFloat timeLB_W = sw-self.nickNameLb.rb_right - 10;
    self.timeLb.frame = CGRectMake(self.nickNameLb.rb_right, 5, timeLB_W, 22);
    self.starView.frame = CGRectMake(62, 30, 100,30);
    
    //高度自适应:不给高
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nickNameLb.mas_left);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.top.equalTo(self.div1.mas_bottom).offset(10);
        make.bottom.equalTo(self.picClv.mas_top).offset(-10);
    }];

    //图片clv 高度自适应    60|<------->|10
    [self.picClv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentLb.mas_left);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    
    //线
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.picClv.mas_bottom);
    }];
}


#pragma mark - getter
- (UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
//        _div1.backgroundColor = [UIColor yellowColor];
    }
    return _div1;
}

- (UIImageView *)iconIV {
    if (!_iconIV) {
        _iconIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"默认头像"]];
        //不要假数据
        _iconIV.backgroundColor = [UIColor lightGrayColor];
        _iconIV.layer.cornerRadius = 22.5; //size(45,45)
        
        _iconIV.layer.masksToBounds = YES;
    }
    return _iconIV;
}

- (UILabel *)nickNameLb {
    if (!_nickNameLb) {
        _nickNameLb = [UILabel new];
        _nickNameLb.text = @"放风筝的人";
    }
    return _nickNameLb;
}
- (UILabel *)timeLb {
    if (!_timeLb) {
        _timeLb = [UILabel new];
        _timeLb.text = @"2018-07-06";
        _timeLb.textAlignment = NSTextAlignmentRight;
        _timeLb.textColor = [UIColor lightGrayColor];
    }
    return _timeLb;
}
- (XHStarRateView *)starView {
    if (!_starView) {
        _starView = [[XHStarRateView alloc] initWithFrame:CGRectMake(60, 26, 100,30)];
        _starView.userInteractionEnabled = NO; //关闭交互,不让点击
    }
    return _starView;
}

- (UILabel *)contentLb {
    if (!_contentLb) {
        _contentLb = [UILabel new];
        _contentLb.text = @"125410dfkjdflkjalkfje|fjdsklfjakldsjf";
        _contentLb.numberOfLines = 0;
//        _contentLb.backgroundColor = [UIColor redColor];
//        _contentLb.font = [UIFont systemFontOfSize:13.0];
    }
    return _contentLb;
}

- (NSMutableArray *)picArrM {
    if (!_picArrM) {
        _picArrM = [NSMutableArray array];
    }
    return _picArrM;
}


#pragma mark - 图片clv
- (UICollectionView *)picClv {
    if (!_picClv) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _picClv = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _picClv.delegate = self;
        _picClv.dataSource = self;
        _picClv.backgroundColor = [UIColor whiteColor];
        //注册cell
        [_picClv registerClass:[EvaluatePicItem class] forCellWithReuseIdentifier:@"EvaluatePicItem"];
    }
    return _picClv;
}

- (UIView *)bottomLineView {
    if (!_bottomLineView) {
        _bottomLineView = [UIView new];
        _bottomLineView.backgroundColor = kMainLightGrayColor;
    }
    return _bottomLineView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArrM.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    EvaluatePicItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"EvaluatePicItem" forIndexPath:indexPath];
    
    UIImage *placeImg = [UIImage imageNamed:@"placeholderImage"]; //占位图片
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kPicDomain,self.picArrM[indexPath.row]];
    NSURL *url = [NSURL URLWithString:urlStr];
    [item.iv sd_setImageWithURL:url placeholderImage:placeImg];
    return item;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat clvW = (SCREEN_WIDTH - 60 - 10);
    
    
    //尽量把间距写大一点不然, 超一点都不行吗，就会被挤下来。
    CGFloat w = (clvW - 3*3) / 4;   //✅
//    CGFloat w = (clvW - 2*3) / 4; //❎
    return CGSizeMake(w, w);
}


//坑:多一点都不行，都会被挤下来。
//左右之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2;
}


#pragma mark - 赋值
- (void)setEvaluateModel:(SDEvaluateModel *)evaluateModel {
    _evaluateModel = evaluateModel;
    
    //设置头像
    [self.iconIV sd_setImageWithURL:[NSURL URLWithString:evaluateModel.head]];
    
    self.nickNameLb.text = evaluateModel.nickname;
    self.timeLb.text = [NSString timeStrFromTimeStamp:[NSString stringWithFormat:@"%@",evaluateModel.createTime]];
    
    //✨星星数量
    self.starView.currentRating = [evaluateModel.complexStarLevel floatValue];
    
    self.contentLb.text = evaluateModel.content;
    
    self.picArrM = [evaluateModel.thumbnail mutableCopy];
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.picClv reloadData];
    });
}
@end

/**
 1.返回的内容不一样,布局的时候怎么根据内容进行布局。
 2.cell的高度可以根据model返回的内容进行计算。
 
 */

